package com.eatme.web.sys.quartz.listener;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Trigger;
import org.quartz.listeners.JobListenerSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

public class AppJobListener extends JobListenerSupport {

    private static final Logger LOG = LoggerFactory.getLogger(AppJobListener.class);

    @Override
    public String getName() {
        return "appJobListener";
    }

    /**
     * job即将被执行
     * @param context
     */
    @Override
    public void jobToBeExecuted(JobExecutionContext context) {
        super.jobToBeExecuted(context);
        LOG.info("时间: {},jobToBeExecuted!", DateFormatUtils.format(new Date(),DateFormatUtils.ISO_DATETIME_FORMAT.getPattern()));
    }

    /**
     * 记录本次job被否决({@link AppTriggerListener#vetoJobExecution(Trigger, JobExecutionContext) } vetoJobExecution被否决的时候会执行本方法)
     * @param context
     */
    @Override
    public void jobExecutionVetoed(JobExecutionContext context) {
        super.jobExecutionVetoed(context);
        LOG.info("时间: {},jobExecutionVetoed!", DateFormatUtils.format(new Date(),DateFormatUtils.ISO_DATETIME_FORMAT.getPattern()));
    }

    /**
     * job已经被执行
     * @param context
     * @param jobException
     */
    @Override
    public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
        super.jobWasExecuted(context, jobException);
        LOG.info("时间: {},jobWasExecuted!", DateFormatUtils.format(new Date(),DateFormatUtils.ISO_DATETIME_FORMAT.getPattern()));
    }
}
